package com.itheima.admin.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.DeptDto;
import com.itheima.admin.pojo.Dept;
import com.itheima.admin.service.IDeptService;
import com.itheima.admin.vo.DeptPageVo;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @ApiOperation("部门条件分页查询")
    @PostMapping("/list")
    public PageVo<DeptPageVo> queryByPage(@RequestBody DeptDto deptDto) {
        if (deptDto.getCurrentPage() == null) {
            deptDto.setCurrentPage(1);
        }
        if (deptDto.getPageSize() == null) {
            deptDto.setPageSize(10);
        }
        return deptService.queryByPage(deptDto);
    }

    @ApiOperation("查询所有的部门")
    @GetMapping("/listall")
    public List<Dept> selectAll() {
        List<Dept> depts = deptService.selectAll();
        return depts;
    }

    @ApiOperation("添加部门")
    @PostMapping("/add")
    public Result insertDept(@RequestBody Dept dept) {
        boolean result = deptService.insertDept(dept);
        return new Result(result, result ? "添加部门成功" : "添加部门失败", null);
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/{id}")
    public Result deleteDeptById(String id) {
        boolean result = deptService.deleteDeptById(id);
        return new Result(result, result ? "删除部门成功" : "删除部门失败", null);
    }

    @ApiOperation("根据id查询部门")
    @GetMapping("/{id}")
    public Dept selectDeptById(@PathVariable("id") String id) {
        return deptService.selectDeptById(Integer.parseInt(id));
    }

    @ApiOperation("修改部门")
    @PutMapping("/edit")
    public Result updateDept(@RequestBody Dept dept) {
        boolean result = deptService.updateDept(dept);
        return new Result(result, result ? "更新部门成功" : "更新部门失败", null);

    }
}
